//百度转高德
function bd_decrypt(bd_lng, bd_lat) {
    var X_PI = Math.PI * 3000.0 / 180.0;
    var x = bd_lng - 0.0065;
    var y = bd_lat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
    var gg_lng = z * Math.cos(theta);
    var gg_lat = z * Math.sin(theta);

    return [gg_lng, gg_lat];

}

//高德坐标转百度（传入经度、纬度）
function bd_encrypt(gg_lng, gg_lat) {
    var X_PI = Math.PI * 3000.0 / 180.0;
    var x = gg_lng, y = gg_lat;
    var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
    var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
    var bd_lng = z * Math.cos(theta) + 0.0065;
    var bd_lat = z * Math.sin(theta) + 0.006;


    return [bd_lng, bd_lat];

}

//百度转火星
var bdTogcj = function (lat, lng) {

    var x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    var x = lng - 0.0065;

    var y = lat - 0.006;

    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);

    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);

    var g2_lnn = z * Math.cos(theta);

    var g2_lat = z * Math.sin(theta);

    return [g2_lat, g2_lnn]

}
var gcjTobd = function (g2_lat, g2_lng) {

    var x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    var z = Math.sqrt(g2_lng * g2_lng + g2_lat * g2_lat) + 0.00002 * Math.sin(g2_lat * x_pi);

    var theta = Math.atan2(g2_lat, g2_lng) + 0.000003 * Math.cos(g2_lng * x_pi);

    var bd_lng = z * Math.cos(theta) + 0.0065;

    var bd_lat = z * Math.sin(theta) + 0.006;

    return [bd_lng, bd_lat]

}

//aoi坐标集排序与分组
var aoi_coords = function (data) {
    res = []
    try {
        if (data.indexOf("|") > 0) {
            data = data.split('|');
            for (i = 0; i < data.length; i++) {

                data_coords = data[i].split('_');
                var data_coords_res = [];
                for (z = 0; z < data_coords.length; z++) {
                    data_coord = data_coords[z].split(',')
                    for (y = 0; y < data_coord.length; y = y + 2) {

                        data_coords_res.push(
                            bd_encrypt(parseFloat(data_coord[y]), parseFloat(data_coord[y + 1]))
                        )
                    }
                }
                res.push(data_coords_res)
            }
        } else {
            data_coords = data.split('_');
//					    console.log("这是第二套方法");
            for (z = 0; z < data_coords.length; z++) {
                data_coord = data_coords[z].split(',')
                for (y = 0; y < data_coord.length; y = y + 2) {
                    res.push(
                        bd_encrypt(parseFloat(data_coord[y]), parseFloat(data_coord[y + 1]))
                    )
                }
            }
        }
    } catch (err) {
        console.log(err);
    } finally {
        return res;
    }
}

//  ";"拆分的数据
var bounds_Group = function (data) {
    res = [];
    data = data.split(';');
    for (i = 0; i < data.length; i = i + 2) {
        pt = [parseFloat(data[i]), parseFloat(data[i + 1])]
        res.push([pt])
    }
    return res;
}

// 比例计算方法
var scale_Zoom = function (data) {
    var scale = 0
//				console.log(data);
//				console.log(JSON.stringify(data));
    try {
        for (i = 0; i < data.lenght - 1; i++) {
            scale = sqrt(pow(data[i][0] - data[i + 1][0], 2) + pow(data[i][1] - data[i + 1][1], 2)) * 1000
//						console.log(scale)
        }
    } catch (error) {
        console.log(error)
    }
    if (scale <= 10) return 19;
    else if (scale <= 25) return 18;
    else if (scale <= 50) return 17;
    else if (scale <= 100) return 16;
    else if (scale <= 200) return 15;
    else if (scale <= 500) return 14;
    else if (scale <= 1000) return 13;
    else if (scale <= 2000) return 12;
    else if (scale <= 5000) return 11;
    else if (scale <= 10000) return 10;
    else if (scale <= 20000) return 9;
    else if (scale <= 30000) return 8;
    else if (scale <= 50000) return 7;
    else if (scale <= 100000) return 6;
    else if (scale <= 200000) return 5;
    else if (scale <= 500000) return 4;
    else if (scale <= 1000000) return 3;
    else if (scale > 1000000) return 2;
}

//判断点是否在平面中
function isPointInPolygon(point, polygon) {
    var N = polygon.length;
    var boundOrVertex = true; //如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
    var intersectCount = 0; //cross points count of x 
    var precision = 2e-10; //浮点类型计算时候与0比较时候的容差
    var p1, p2; //neighbour bound vertices
    var p = point; //测试点

    p1 = polygon[0]; //left vertex        
    for (var i = 1; i <= N; ++i) { //check all rays            
        if (p.x == p1.x && p.y == p1.y) {
            return boundOrVertex; //p is an vertex
        }

        p2 = polygon[i % N]; //right vertex            
        if (p.y < Math.min(p1.y, p2.y) || p.y > Math.max(p1.y, p2.y)) { //ray is outside of our interests                
            p1 = p2;
            continue; //next ray left point
        }

        if (p.y > Math.min(p1.y, p2.y) && p.y < Math.max(p1.y, p2.y)) { //ray is crossing over by the algorithm (common part of)
            if (p.x <= Math.max(p1.x, p2.x)) { //x is before of ray                    
                if (p1.y == p2.y && p.x >= Math.min(p1.x, p2.x)) { //overlies on a horizontal ray
                    return boundOrVertex;
                }

                if (p1.x == p2.x) { //ray is vertical                        
                    if (p1.x == p.x) { //overlies on a vertical ray
                        return boundOrVertex;
                    } else { //before ray
                        ++intersectCount;
                    }
                } else { //cross point on the left side                        
                    var xinters = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x; //cross point of x                        
                    if (Math.abs(p.x - xinters) < precision) { //overlies on a ray
                        return boundOrVertex;
                    }

                    if (p.x < xinters) { //before ray
                        ++intersectCount;
                    }
                }
            }
        } else { //special case when ray is crossing through the vertex                
            if (p.y == p2.y && p.x <= p2.x) { //p crossing over p2                    
                var p3 = polygon[(i + 1) % N]; //next vertex                    
                if (p.y >= Math.min(p1.y, p3.y) && p.y <= Math.max(p1.y, p3.y)) { //p.y lies between p1.y & p3.y
                    ++intersectCount;
                } else {
                    intersectCount += 2;
                }
            }
        }
        p1 = p2; //next ray left point
    }

    if (intersectCount % 2 == 0) { //偶数在多边形外
        return false;
    } else { //奇数在多边形内
        return true;
    }
}

//地点名的坐标格式转换
//格式转换,用于isPointInPolygon方法  ， 方法需要优化
var z_h_zb = function (data, i) {
    res = {};
    var geoCoord = data[i]['value'];
    if (geoCoord) {
        res = {
            x: geoCoord[0],
            y: geoCoord[1]
        };
    }
    return res;
};
//地图面积的坐标转换
//格式转换用于isPointInPolygon方法v  , 方法需要优化     [[3.33,44.44],[454.534,5433.52]]   转 [{3.33,44.44},{454.534,5433.52}]
var z_h_coo = function (data) {
    res = [];
    for (i = 0; i < data.length; i++) {
        res.push({
            x: data[i][0],
            y: data[i][1]
        });
    }
    return res;
};

var strSimilarity2Number = function (s, t) {
    var n = s.length, m = t.length, d = [];
    var i, j, s_i, t_j, cost;
    if (n == 0) return m;
    if (m == 0) return n;
    for (i = 0; i <= n; i++) {
        d[i] = [];
        d[i][0] = i;
    }
    for (j = 0; j <= m; j++) {
        d[0][j] = j;
    }
    for (i = 1; i <= n; i++) {
        s_i = s.charAt(i - 1);
        for (j = 1; j <= m; j++) {
            t_j = t.charAt(j - 1);
            if (s_i == t_j) {
                cost = 0;
            } else {
                cost = 1;
            }
            d[i][j] = self.Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
        }
    }
    return d[n][m];
};
//两个字符串的相似程度，并返回相似度百分比
var strSimilarity2Percent = function (s, t) {
    var l = s.length > t.length ? s.length : t.length;
    var d = self.strSimilarity2Number(s, t);
    return (1 - d / l).toFixed(4);
};
var Minimum = function (a, b, c) {
    return a < b ? (a < c ? a : c) : (b < c ? b : c);
};